/**
 * shopmobile for tpshop
 * ============================================================================
 * 版权所有 2015-2099 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ——————————————————————————————————————
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 飞龙  wangqh01292@163.com
 * Date: @date 2015年10月20日 下午7:19:26 
 * Description:购物车Fragment
 * @version V1.0
 */
package org.gangcai.mac.shop.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.activity.WebActivity;
import org.gangcai.mac.shop.adapter.AnimationAdapter1;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.HomeDataBean;
import org.gangcai.mac.shop.bean.PostsArticleBaseBean;
import org.gangcai.mac.shop.manager.RetrofitManageres;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.rxlife.RxFragment;
import org.gangcai.mac.shop.utils.PromptDialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import me.leefeng.promptlibrary.PromptDialog;


/**
 *  首页 -> 发布信息首页
 *
 */
public class PublicReportFragment extends RxFragment implements OnRefreshListener,View.OnClickListener{
	private static final String ARG_PARAM1 = "param1";
	private static final String ARG_PARAM2 = "param2";
	private String TAG = "SPReplayFragment";
	private Context mContext;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.replay_title_text)
    TextView replay_title_text;
	@BindView(R.id.recyclerview_replay_view)
    RecyclerView recyclerview_replay_view;
	Unbinder unbinder;
    private AnimationAdapter1 animationAdapter1;
    private List<PostsArticleBaseBean> dataList=new ArrayList<PostsArticleBaseBean>(  );
    private View headView;
    private PromptDialog prompDialog;
    private static final int MENU_SIZE = 10;
    private int mNextRequestPage = 1;
    private boolean hasHeadView;
	public PublicReportFragment() {
	}

	public static PublicReportFragment newInstance(String param1, String param2) {
		PublicReportFragment fragment = new PublicReportFragment();
		Bundle args = new Bundle();
		args.putString(ARG_PARAM1, param1);
		args.putString(ARG_PARAM2, param2);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mContext = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.shopreplay_view, container,false);
		unbinder = ButterKnife.bind( this, view );
		return view;
	}

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        prompDialog = PromptDialogUtils.getPrompDialog(getActivity());
        headView = getLayoutInflater().inflate(R.layout.replay_head_view, (ViewGroup) recyclerview_replay_view.getParent(), false);
        initView();
        loadData();
        int layout_id=R.id.layout_01;
        for(int i=0;i<MENU_SIZE;i++){
            LinearLayout linearLayout=(LinearLayout)headView.findViewById(layout_id+i);
            linearLayout.setOnClickListener( this );
        }

    }

    private void initView() {
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnRefreshListener(this);
        animationAdapter1 = new AnimationAdapter1(getActivity(),dataList);
        animationAdapter1.openLoadAnimation();
        animationAdapter1.setNotDoAnimationCount(3);
        recyclerview_replay_view.setLayoutManager(new GridLayoutManager(getActivity(), 1));

        animationAdapter1.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {

            }
        });
        animationAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PostsArticleBaseBean postsArticleBaseBean=dataList.get( position );
                Bundle bundle = new Bundle();
                String postID=postsArticleBaseBean.getObject_id();
                String cid=postsArticleBaseBean.getTerm_id();
                String webString="http://thinkcmf.500-china.com/index.php?g=&m=article&a=index&id="+postID+"&cid="+cid;
                bundle.putString("webString", webString);
                bundle.putString("title" , postsArticleBaseBean.getPost_title());
                bundle.putString("postID" , postID);
                ActivityUtils.startActivity(bundle, getActivity(), WebActivity.class);
            }
        });
        animationAdapter1.addHeaderView(headView);
        recyclerview_replay_view.setAdapter(animationAdapter1);
    }

    private void loadData() {
        LogUtils.e("loadHomeFragment");
        mNextRequestPage = 1;
        RetrofitManageres.getInstance().create(AppAPI.class).ReplayData("Portal","list","indexReplayJson")
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<HomeDataBean>bindToLifecycle())
                .subscribe(new CommonObserver<HomeDataBean>(refreshLayout) {
                    @Override
                    public void onSuccess(HomeDataBean value) {
                        if (value.getCode() == 101) {
                            dataList=value.getReferer().getPosts();
                            animationAdapter1.setNewData(dataList);
                            animationAdapter1.loadMoreComplete();
                        }
                    }

                });

    }

    private void selectLoadData(String cid) {
        LogUtils.e("loadHomeFragment");
        mNextRequestPage = 1;
        RetrofitManageres.getInstance().create(AppAPI.class).HomeData("Portal","list","indexJson",cid)
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<HomeDataBean>bindToLifecycle())
                .subscribe(new CommonObserver<HomeDataBean>(prompDialog) {
                    @Override
                    public void onSuccess(HomeDataBean value) {
                        if (value.getCode() == 101) {
                            dataList=value.getReferer().getPosts();
                            animationAdapter1.setNewData(dataList);
                            animationAdapter1.loadMoreComplete();
                        }
                    }

                });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}


	@Override
	public void onDetach() {
		super.onDetach();
	}


	@Override
	public void onPause() {
		super.onPause();

	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		Log.i( "Info", "onResult:" + requestCode + " onResult:" + resultCode );
		super.onActivityResult( requestCode, resultCode, data );
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onStart() {
		super.onStart();
	}


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_01:
                selectLoadData("7");
                replay_title_text.setText( "板材" );
                break;
            case R.id.layout_02:
                selectLoadData("8");
                replay_title_text.setText( "型材" );
                break;
            case R.id.layout_03:
                selectLoadData("9");
                replay_title_text.setText( "管材" );
                break;
            case R.id.layout_04:
                selectLoadData("10");
                replay_title_text.setText( "建材" );
                break;
            case R.id.layout_05:
                selectLoadData("11");
                replay_title_text.setText( "废钢" );
                break;
            case R.id.layout_06:
                selectLoadData("12");
                replay_title_text.setText( "钢结构" );
                break;
            case R.id.layout_07:
                selectLoadData("13");
                replay_title_text.setText( "原料" );
                break;
            case R.id.layout_08:
                selectLoadData("14");
                replay_title_text.setText( "工程机械" );
                break;
            case R.id.layout_09:
                selectLoadData("15");
                replay_title_text.setText( "其他" );
                break;
            default:
                selectLoadData("7");
                replay_title_text.setText( "板材" );
                break;
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        replay_title_text.setText( "城库报价" );
        loadData();
    }
}
